<template>
  <div>
    <el-card class="operator-button-card">
      <template #header>
        <div class="flex justify-between items-center">
          <h2 class="operator-button-label">{{ $t('operator.functionalConfiguration') }}</h2>
            <el-button
              v-if="isAuth('10400202.MANAGE')"
              type="primary"
              size="small"
              :loading="functionLoading"
              @click="saveFunctionHandle"
              >{{ $t('common.save') }}</el-button
            >
        </div>
      </template>
      <el-form :model="functionData" label-width="195px">
        <el-form-item>
          <el-switch
            v-model="functionData.sysAppautoUpdateSwitch"
            :active-text="$t('operator.config.sysAppAutoUpdate')"
            :active-value="0"
            :inactive-value="1"
          />
        </el-form-item>
        <el-form-item>
          <el-switch
            v-model="functionData.checkAppSignSwitch"
            :active-text="$t('operator.config.appSign')"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>
        <el-form-item>
          <el-switch
            v-model="functionData.geoTraceSwitch"
            :active-text="$t('operator.config.geoTrace')"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>
        <el-form-item v-show="merchantServiceSwitchShow">
          <el-switch
            v-model="functionData.merchantServiceSwitch"
            :disabled="receiveMerchantServiceSwitch === 1"
            :active-text="$t('operator.config.merchantService')"
            :active-value="1"
            :inactive-value="0"
          ></el-switch>
        </el-form-item>
        <el-form-item>
          <el-switch
            v-model="functionData.flyKeyCertDownloadSwitch"
            :active-text="$t('operator.config.flyKeySwitch')"
            :active-value="1"
            :inactive-value="0"
          ></el-switch>
        </el-form-item>
        <el-form-item>
          <el-switch
            v-model="functionData.terminalDynamicPasswordSwitch"
            :active-text="$t('operator.config.terminalDynamicPasswordSwitch')"
            :active-value="1"
            :inactive-value="0"
          ></el-switch>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { getConfigure, configure } from '@/api/operator'
import { isAuth } from '@/utils'

export default {
  props: {
    operatorId: {
      require: true,
      type: String,
      default: null
    }
  },
  data() {
    return {
      functionLoading: false,
      functionData: {
        sysAppautoUpdateSwitch: 1,
        checkAppSignSwitch: 1,
        geoTraceSwitch: 0,
        merchantServiceSwitch: 0,
        flyKeyCertDownloadSwitch: 0,
        terminalDynamicPasswordSwitch: 0
      },
      operator: null,
      receiveMerchantServiceSwitch: 1,
      merchantServiceSwitchShow: true
    }
  },
  mounted() {
    this.getConfigure(this.operatorId)
    this.operator = JSON.parse(localStorage.getItem('operator-info'))
    console.log(this.operator)
    // �ȼ���Ϊ��̬��ȡ������д��
    // if (this.operator.levelCode !== 'CHARGE_L2') {
    //   this.merchantServiceSwitchShow = false
    // }
  },
  methods: {
    getConfigure(operatorId) {
      getConfigure(operatorId)
        .then((data) => {
          console.log(data)
          if (data) {
            this.functionData.sysAppautoUpdateSwitch =
              data.sysAppautoUpdateSwitch === null
                ? 1
                : Number(data.sysAppautoUpdateSwitch)
            this.functionData.checkAppSignSwitch =
              data.checkAppSignSwitch === null
                ? 1
                : Number(data.checkAppSignSwitch)
            this.functionData.geoTraceSwitch =
              data.geoTraceSwitch === null ? 0 : Number(data.geoTraceSwitch)
            this.functionData.merchantServiceSwitch =
              data.merchantServiceSwitch === null
                ? 0
                : Number(data.merchantServiceSwitch)
            this.functionData.flyKeyCertDownloadSwitch =
              data.flyKeyCertDownloadSwitch === null
                ? 0
                : Number(data.flyKeyCertDownloadSwitch)
            this.functionData.terminalDynamicPasswordSwitch =
              data.terminalDynamicPasswordSwitch === null
                ? 0
                : Number(data.terminalDynamicPasswordSwitch)
            this.receiveMerchantServiceSwitch =
              this.functionData.merchantServiceSwitch
          } else {
            this.receiveMerchantServiceSwitch = 0
          }
        })
        .catch((e) => {
          this.$message.error(e.message)
        })
    },
    saveFunctionHandle() {
      this.$confirm(this.$t('operator.save_cfg'), this.$t('page.tip'), {
        confirmButtonText: this.$t('page.confirm'),
        cancelButtonText: this.$t('page.cancel'),
        type: 'warning'
      })
        .then(() => {
          this.functionLoading = true
          configure(this.operatorId, this.functionData)
            .then((res) => {
              this.getConfigure(this.operatorId)
              this.receiveMerchantServiceSwitch = 1
              this.$message.success(this.$t('common.success'))
              this.functionLoading = false
            })
            .catch((err) => {
              this.$confirmError(err)
              this.getConfigure(this.operatorId)
              this.functionLoading = false
            })
        })
        .catch((e) => {})
    },
    isAuth
  }
}
</script>

<style lang="scss" scoped>

</style>
